package com.example.lawyer;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.provider.FontRequest;
import androidx.databinding.DataBindingUtil;
import androidx.emoji.bundled.BundledEmojiCompatConfig;
import androidx.emoji.text.EmojiCompat;
import androidx.emoji.text.FontRequestEmojiCompatConfig;

import com.example.lawyer.databind.activity.ActivitryChatDataBindModel;
import com.example.lawyer.databind.assembly.GeneralTopTitleData;
import com.example.lawyer.databind.itemview.ItemBiaoQingData;
import com.example.lawyer.databinding.ActivityChatBinding;
import com.example.lawyer.netty.NettyStart;
import com.example.lawyer.netty.activityhandler.ActivityMessageCallback;
import com.example.lawyer.service.NettyService;

import java.util.ArrayList;
import java.util.List;

public class ChatActivity extends AppCompatActivity {

    private ActivityChatBinding chatBinding;
    ActivitryChatDataBindModel chatDataBindModel;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initEmojiCompats();


        chatBinding = DataBindingUtil.setContentView(this, R.layout.activity_chat);

        chatDataBindModel = new ActivitryChatDataBindModel();
        GeneralTopTitleData generalTopTitleData = new GeneralTopTitleData();
        generalTopTitleData.isshow.set(true);
        generalTopTitleData.title.set("聊天");
        chatDataBindModel.GeneralTop.set(generalTopTitleData);
        chatDataBindModel.isbiaoqingshow.set(false);
        chatBinding.setChatbindmodel(chatDataBindModel);

        initbiaoqing();


        StartService();


        handlerThread.start();
        notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        Intent intens = new Intent(getApplicationContext(),NettyService.class);
        getApplicationContext().stopService(intens);
    }

    private NotificationManager notificationManager;
    HandlerThread handlerThread =  new HandlerThread("notia");

    Handler handler  =   new Handler(Looper.getMainLooper()){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            Toast.makeText(getApplicationContext(),"收到消息",Toast.LENGTH_LONG).show();

        }
    };



    private void initbiaoqing() {
        List<ItemBiaoQingData> listid = new ArrayList<>();

        for (int s = 0; s < 300; s++) {
            ItemBiaoQingData itemBiaoQingData = new ItemBiaoQingData();
            itemBiaoQingData.rid.set(R.mipmap.astonished_face_3d);
            listid.add(itemBiaoQingData);
        }

        chatDataBindModel.biaoqinglist.set(listid);

    }

    private void initEmojiCompats() {

        EmojiCompat.Config compat = new BundledEmojiCompatConfig(this);
        compat.setReplaceAll(true);
        compat.registerInitCallback(new EmojiCompat.InitCallback() {
            @Override
            public void onInitialized() {
                super.onInitialized();
                System.out.println("初始化成功1");
            }

            @Override
            public void onFailed(@Nullable Throwable throwable) {
                super.onFailed(throwable);
                System.out.println("初始化失败1");
            }
        });
        EmojiCompat.init(compat);
    }
    private ServiceConnection connection = new ServiceConnection() {
        @Override//服务链接
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            Log.i("RES", "服务链接----------------------");
            NettyService.MyBindder ss = (NettyService.MyBindder) iBinder;
            NettyService myBind = ss.getMyBind();
            myBind.SetOnSendActivtyListener(new NettyService.SendActivityMessage() {

                @Override
                public void ReceiveMsg(String str,String type) {
                    System.out.println("ChatActivity收到来自Netty的消息");
                    System.out.println(str);
                    Message obtain = Message.obtain();
                    obtain.what = 96527;
                    obtain.obj = str;
                    handler.sendMessage(obtain);
                }
            });
        }

        @Override //断开链接
        public void onServiceDisconnected(ComponentName componentName) {
            Log.i("RES", "断开链接");
        }
    };
    private void StartService() {
        Intent intent = new Intent(this.getApplicationContext(), NettyService.class);
        bindService(intent, connection, BIND_AUTO_CREATE);
        startService(intent);
    }



}
